﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using tpos_business;
using app_common;
using tpos_custom_control;


namespace tpos_front_end
{
    public partial class frm_front_end_groupitem : Form
    {
        clsGroupItem clsB = new clsGroupItem();
        DataTable dtGroupItem = null;
        int BWIDTH = 75;
        int BHEIGHT = 60;
        int PADDING = 5;

        int panelActive = 0;
        int panelNumber = 1;

        string tableCode = "";
        Panel[] itemGroupPanel = null;

        public frm_front_end_groupitem(string _tableCode)
        {
            InitializeComponent();
            tableCode = _tableCode;
        }

        private void frm_front_end_groupitem_Load(object sender, EventArgs e)
        {
            loadGroupItem();
            //MessageBox.Show(this.panel2.Width.ToString());
        }

        private void loadGroupItem()
        {
            try
            {
                dtGroupItem = clsB.getGroupItem();
                if (dtGroupItem != null)
                {
                    int panelWidth = this.panelItemGroup01.Width-100;
                    int rows = dtGroupItem.Rows.Count;
                    int btnOnAPanel = panelWidth / (BWIDTH + PADDING);//Số lượng button trên 1 panel.                    
                    if (rows % btnOnAPanel == 0)
                        panelNumber = rows / btnOnAPanel;
                    else
                    {
                        panelNumber = rows / btnOnAPanel;
                        panelNumber += 1;
                    }
                    itemGroupPanel = new Panel[panelNumber];                    

                    for (int j = 0; j < panelNumber; j++)
                    {
                        itemGroupPanel[j] = new Panel();
                        itemGroupPanel[j].Location = new Point(0, 0);
                        itemGroupPanel[j].Height = 60;
                        itemGroupPanel[j].Width = panelWidth;
                        //itemGroupPanel[j].Dock = DockStyle.Fill;
                        itemGroupPanel[j].BackColor = Color.Blue;
                        this.panelItemGroup01.Controls.Add(itemGroupPanel[j]);
                        //itemGroupPanel[j].BringToFront();
                        if (j == 0)
                        {
                            itemGroupPanel[j].Visible = true;
                        }
                        else
                        {
                            itemGroupPanel[j].Visible = false;
                        }

                        /*
                        if (j == 0)
                            itemGroupPanel[j].Visible = true;
                        else
                            itemGroupPanel[j].Visible = false;
                        */

                        for (int i = j * btnOnAPanel; i < (j + 1) * btnOnAPanel; i++)
                        {
                            if (i < rows)
                            {
                                uc_ButtonLable[] btn = new uc_ButtonLable[rows];
                                btn[i] = new uc_ButtonLable();
                                btn[i].bWidth = BWIDTH;
                                btn[i].bHigh = BHEIGHT;
                                if (i == 0)
                                    btn[i].Location = new Point(5, 0);
                                else
                                {
                                    int x = i - (j * btnOnAPanel);
                                    if(x==0)
                                        btn[i].Location = new Point(x * BWIDTH+5 , 0);//Neu la button dau tien tren panel thi khong can cong Padding
                                    else
                                        btn[i].Location = new Point(x * BWIDTH + PADDING, 0);
                                }
                                btn[i].bText = dtGroupItem.Rows[i]["name"].ToString();
                                
                                itemGroupPanel[j].Controls.Add(btn[i]);
                            }
                        }
                    }

                    btnPre.Enabled = false;
                    if (panelNumber == 1)
                    {
                        btnNext.Enabled = false;                        
                    }
                }
                else
                {
                    MessageBox.Show("Không thể load danh sách group item.", STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch { }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            panelActive += 1;
            for (int i = 0; i < panelNumber; i++)
            {
                itemGroupPanel[i].Visible = false;
            }
            itemGroupPanel[panelActive].Visible = true;

            if (panelActive == panelNumber-1)
            {
                btnNext.Enabled = false;
            }
            if (panelActive > 0)
            {
                btnPre.Enabled = true;
            }
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            panelActive -= 1;
            for (int i = 0; i < panelNumber; i++)
            {
                itemGroupPanel[i].Visible = false;
            }
            itemGroupPanel[panelActive].Visible = true;
            if (panelActive == 0)
            {
                btnPre.Enabled = false;
            }
            if (panelActive < panelNumber - 1)
                btnNext.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void loadItems(int itemGroup)
        {
            int itemWidth = 80;
            int itemHeight = 80;
            int itemNumber = 60;
            int panelWidth = panel5.Width;
            int panelHeight = panel5.Height;
            int itemPadding = 10;

            int itemCols = panelWidth / (itemWidth + itemPadding);
            int itemRows = panelHeight / (itemHeight + itemPadding);
            int itemOnPanel = itemCols * itemRows;

            int itemPanel

            uc_ButtonLable[] btnItem = new uc_ButtonLable[itemNumber];


            for (int i = 0; i < itemNumber; i++)
            {

            }
        }
    }
}
